package com.controller.bean;

import com.entites.Employee;
import com.entites.EventLibrary;
import com.entites.EventRegistration;
import com.entites.Notifications;
import com.model.bean.EmployeeFacade;
import com.model.bean.EmployeeFacadeLocal;
import com.model.bean.EventLibraryFacade;
import com.model.bean.EventLibraryFacadeLocal;
import com.model.bean.EventRegistrationFacade;
import com.model.bean.EventRegistrationFacadeLocal;
import com.model.bean.NotificationTypeFacadeLocal;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author NguyenVan
 */
@ManagedBean
@RequestScoped
public class EventLibraryController {

    @EJB
    private EmployeeFacadeLocal employeeFacade;

    @EJB
    private EventLibraryFacadeLocal eventLibraryFacade;

    @EJB
    private EventRegistrationFacadeLocal eventRegistrationFacadeLocal;

    EventLibrary eventLibrary;
    Employee employee;
    private final String NAMEATRIBUTE = "NotificationContent";

    public EventLibraryController() {
        employee = new Employee();
        eventLibrary = new EventLibrary();

    }
    private final String USERNAME = "username";
    private final String DATEFORMAT = "yyyy/MM/dd HH:mm:ss";
    private final String LOGINSESSION = "login";
    private int pages;
    private int currentPages;
    private int noOfRecords;
    private int noOfPage;
    private int eventId;

    private String username;

    FacesContext fCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true);

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public int getEventId() {
        return eventId;
    }

    public void setEventId(int eventId) {
        this.eventId = eventId;
    }

    public int getCurrentPages() {
        return currentPages;
    }

    public void setCurrentPages(int currentPages) {
        this.currentPages = currentPages;
    }

    public int getNoOfRecords() {
        return noOfRecords;
    }

    public void setNoOfRecords(int noOfRecords) {
        this.noOfRecords = noOfRecords;
    }

    public int getNoOfPage() {
        return noOfPage;
    }

    public void setNoOfPage(int noOfPage) {
        this.noOfPage = noOfPage;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }
    /**
     * Creates a new instance of EventLibraryController
     */
    List<Employee> listBirthEmpoyee;
    List<EventLibrary> list;
    EventLibrary ev;
    List<Employee> liste = new ArrayList<>();

    public List<Employee> getListe() {
        return liste;
    }

    public void setListe(List<Employee> liste) {
        this.liste = liste;
    }

    public List<EventLibrary> getEventLibrary() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (request.getParameter("pages") != null) {
            pages = Integer.parseInt(request.getParameter("pages"));
        }
        int record = 6;
        currentPages = 1;
        if (pages != 0) {
            currentPages = pages;
        }
        int start_row = (currentPages * record) - (record - 1);
        int end_row = start_row + record - 1;
        list = eventLibraryFacade.findAllEveventLibraryOrderByDesc(start_row, end_row, getUsername());
        noOfRecords = eventLibraryFacade.count();
        noOfPage = ((int) Math.ceil(noOfRecords * 1.0 / record));
        return list;
    }

    public void registerEvent(String eventId) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
        employee.setUsername(getUsername());
        eventLibrary.setEventId(Integer.parseInt(eventId));
        Date date = new Date();
        dateFormat.format(date);
        EventRegistration eventRegistration = new EventRegistration(eventLibrary, employee, date);
        eventRegistration.setId(1);
        eventRegistrationFacadeLocal.create(eventRegistration);
    }

    public void redirectEventDetails(int id) throws IOException {
        session.setAttribute("eventId", id);
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        context.redirect(context.getRequestContextPath() + "/EventDetails.xhtml");

    }

    public String getUsername() {
        return username = (String) session.getAttribute(USERNAME);
    }

    public List<Employee> listEmployeeInvited(int id) {
        liste = employeeFacade.findAllEmployeeTnviteEvent(id,getUsername());
        return liste;
    }

    public EventLibrary getUpcomingEvent() {
        EventLibrary evl = eventLibraryFacade.getUpcomingEvent(getUsername());
        return evl;
    }

}
